<?php

namespace App\Admin\Controllers;

use App\Room;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class RoomController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('房间管理')
            ->description('在这里管理您的房间')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('房间详情')
            ->description('房间详情')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('修改房间')
            ->description('修改房间信息')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('新建房间')
            ->description('添加新房间')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Room);

        $grid->id('房间id');
        $grid->created_at('添加时间');
        $grid->number('房间号码');
        $grid->room_type('类型')->sortable()->display(function($room_type){
            return $room_type['name'];
        });
        $grid->is_reserved('状态')->using([true => '已占用', false => '空闲']);

        $grid->actions(function($actions){
            $actions->disableView();
//            $actions->disableDelete();
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Room::findOrFail($id));

        $show->id('房间id');
        $show->created_at('添加时间');
        $show->number('Number');
        $show->room_type_id('Room type id');
        $show->is_reserved('Is reserved');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Room);

        $form->text('number', 'Number');
        $form->number('room_type_id', 'Room type id');
        $form->switch('is_reserved', 'Is reserved');

        return $form;
    }
}
